package com.wxapp.admin.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.enums.SqlKeyword;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wxapp.admin.api.pojo.entity.RoleDO;
import com.wxapp.admin.api.pojo.vm.role.PRoleDetailVm;
import com.wxapp.admin.api.pojo.vm.role.PRolePageVm;
import com.wxapp.admin.api.pojo.vo.role.QRolePageVo;
import com.wxapp.admin.api.pojo.vo.role.QRoleSaveVo;
import com.wxapp.admin.api.pojo.vo.role.QRoleUpdateVo;
import com.wxapp.admin.service.mapper.RoleMapper;
import com.wxapp.admin.service.service.RoleService;
import com.wxapp.common.constant.base.IBaseService;
import com.wxapp.common.constant.pojo.vm.ResultVm;
import com.wxapp.common.constant.pojo.vo.UserVo;
import com.wxapp.common.util.check.CheckUtil;
import com.wxapp.common.util.condition.ConditionUtil;
import com.wxapp.common.util.copy.CopyUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: lgl
 * @Description 角色信息表管理
 * @Datetime 2022-01-20 16:17:52
 * @Modified By
 */
@Service
@RequiredArgsConstructor
public class RoleServiceImpl extends IBaseService implements RoleService {
    private final RoleMapper roleMapper;
    @Override
    public List<String> getRoles(String id) {
        return roleMapper.getRoles(id);
    }

    @Override
    public ResultVm<String> saveRole(UserVo userVo, QRoleSaveVo qRoleSaveVo) {
        LambdaQueryWrapper<RoleDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RoleDO::getStatus, true).and(res -> res.eq(RoleDO::getRoleName, qRoleSaveVo.getRoleName()).or().eq(RoleDO::getCode, qRoleSaveVo.getCode()));
        var count = roleMapper.selectCount(wrapper);
        if (count > 0) {
            return error("已存在该角色");
        }
        var roleDO = CopyUtil.copyObjectNew(qRoleSaveVo, RoleDO::new, (s, r) -> {
            r.setWork(true);
            r.setCreateBy(userVo.getId());
            return null;
        });
        roleMapper.insert(roleDO);
        return success("保存角色成功");
    }

    @Override
    public ResultVm<String> updateRole(UserVo userVo, QRoleUpdateVo qSaveVo) {
        var roleDO = roleMapper.selectById(qSaveVo.getId());
        if (CheckUtil.isNullObject(roleDO)) {
            return error("没查到角色信息");
        }
        CopyUtil.copyObject(qSaveVo, roleDO, (s, r) -> {
            r.setUpdateBy(userVo.getId());
            return null;
        });
        roleMapper.updateById(roleDO);
        return success("更新角色成功");
    }

    @Override
    public ResultVm<PRoleDetailVm> detailRole(String id) {
        var roleDO = roleMapper.selectById(id);
        if (CheckUtil.isNullObject(roleDO)) {
            return error("没查到角色信息");
        }
        return success(CopyUtil.copyObjectNew(roleDO, PRoleDetailVm::new, null));
    }

    @Override
    public ResultVm<List<PRoleDetailVm>> detailByUser(UserVo userVo, String userId) {
        List<PRoleDetailVm> pRoleDetailVms = roleMapper.detailByUser(userVo, userId);
        return success(pRoleDetailVms);
    }

    @Override
    public ResultVm<Page<PRolePageVm>> pageRole(QRolePageVo qSaveVo) {
        Map<SFunction<RoleDO, ?>, SqlKeyword> condition = new HashMap<>();
        condition.put(RoleDO::getRoleName, SqlKeyword.LIKE);
        var sCondition = ConditionUtil.getSCondition(RoleDO::new, qSaveVo, null, condition,null);
        var page = ConditionUtil.getPage(qSaveVo, RoleDO.class);
        var rolePage = roleMapper.selectPage(page, sCondition);
        return success(CopyUtil.copyPage(rolePage, PRolePageVm::new, null));
    }

    @Override
    public ResultVm<String> deleteRole(String id) {
        var roleDO = roleMapper.selectById(id);
        if (CheckUtil.isNullObject(roleDO)) {
            return error("没查到角色信息");
        }
        roleDO.setStatus(false);
        roleMapper.updateById(roleDO);
        return success("删除角色成功");
    }
}

